{extend name="style/base"/}
{block name="resources"}
<link rel="stylesheet" href="SITEHOME_CSS/member.css">
{/block}
{block name="main"}
<div class="nc-function-search">
	<button class="layui-btn" onclick="addLevel()">添加等级</button>
</div>
<table id="level_list" lay-filter="member_level"></table>

<script type="text/html" id="create_time">
	<span>{{nc.time_to_date(d.create_time)}}</span>
</script>

<script type="text/html" id="sort">
	<label><input type="number" value="{{ d.sort }}" min="0" class="layui-input nc-len-short" onchange="setSort(this, {{ d.level_id }})"/></label>
</script>

<script type="text/html" id="operation">
	<a class="default" lay-event="edit">编辑</a> |
	<a class="default" lay-event="delete">删除</a> <br/>
</script>

<script type="text/html" id="batchOperation">
	<button class="layui-btn layui-btn-primary" lay-event="delete">删除</button>
</script>

{/block}
{block name="script"}
<script>
	var table;
	var layui_form;
	$(function () {
		
		table = new Table({
			elem : '#level_list',
			filter: "member_level",
			url : '{:addon_url("sitehome/member/levellist")}',
			cols : [[
				{type : 'checkbox'},
				{field : 'level_name', width : '30%', title : '等级名称'},
				{field : 'create_time', width : '20%', title : '创建时间', templet: '#create_time'},
				{field : 'sort', width : '15%', title : '排序', templet: '#sort'},
				{title : '操作', toolbar : '#operation', width : '30%'}
			]],
			bottomToolbar: "#batchOperation"
		});
		
		//批量操作
		table.bottomToolbar(function(obj){
			
			if(obj.data.length < 1){
				layer.msg('请选择要操作的数据');
				return;
			}
			
			switch(obj.event){
				case "delete":
					var id_array = new Array();
					for(i in obj.data) id_array.push(obj.data[i].level_id);
					deleteLevel(id_array.toString());
					break;
			}
		});
		
		//监听工具条
		table.tool(function(obj){
			var data = obj.data;
			switch (obj.event) {
				case 'edit':
					editLevel(data);
					break;
				case 'delete':
					deleteLevel(data.level_id);
					break;
			}
		});
	});
	
	/**
	 * 修改会员等级
	 */
	function editLevel(data) {
		$.ajax({
			type : "post",
			url : '{:addon_url("sitehome/member/levelinfo")}',
			async : false,
			data : {'level_id' : data.level_id},
			dataType: 'json',
			success:function(data){
				if (data.code != 0) {
					layer.msg(data.message);
				}
				data = data.data;
				var html = '';
				html += '<div class="layui-form member-form" id="level_info" lay-filter="form">' +
					'<div class="layui-form-item">' +
					'<label class="layui-form-label"><span class="required">*</span>等级名称</label>' +
					'<div class="layui-input-inline">' +
					'<input type="text" name="level_name" id="level_name" value="'+ data.level_name +'" required lay-verify="required" autocomplete="off" class="layui-input nc-len-mid" maxlength="16">' +
					'</div>' +
					'</div>' +
					'<div class="layui-form-item">' +
					'<label class="layui-form-label">排序</label>' +
					'<div class="layui-input-inline">' +
					'<input type="number" name="sort" id="sort" value="'+ data.sort +'" required lay-verify="number" placeholder="请输入序号" autocomplete="off" class="layui-input nc-len-short" min="0" max="100000">' +
					'</div>' +
					'</div>' +
					'<div class="nc-form-row">' +
					'<button class="layui-btn" lay-submit lay-filter="formDome" onclick="formDome(' + data.level_id + ')">保存</button>' +
					'<button type="reset" class="layui-btn layui-btn-primary" onclick="back()">返回</button>' +
					'</div>' +
					'</div>';
				layer.open({
					type: 1,
					title:'修改会员等级',
					skin: 'layer-tips-class',
					area: ['500px'],
					closeBtn: 0,
					content: html,
					success: function (layero, index) {
						formDome();
					}
				})
			}
		})
	}
	
	/**
	 * 删除会员等级
	 */
	function deleteLevel(level_id) {
		layer.confirm('确定删除吗?', {
			btn: ['确定','取消']
		},  function (index) {
			$.ajax({
				type:"post",
				async:false,
				url : '{:addon_url("sitehome/member/deletelevel")}',
				data: {'level_id' : level_id},
				dataType: 'json',
				success:function(res){
					layer.msg(res.message);
					if(res.code == 0){
						table.reload();
					}
				}
			});
		}, function(){
			layer.close();
		});
	}
	
	/**
	 * 添加会员等级
	 */
	function addLevel() {
		var html = '';
		html += '<div class="layui-form member-form" lay-filter="form">' +
			'<div class="layui-form-item">' +
			'<label class="layui-form-label"><span class="required">*</span>等级名称</label>' +
			'<div class="layui-input-inline">' +
			'<input type="text" name="level_name" id="level_name" value="" required lay-verify="required" placeholder="请输入等级名称" autocomplete="off" class="layui-input  nc-len-mid" maxlength="16">' +
			'</div>' +
			'</div>' +
			'<div class="layui-form-item">' +
			'<label class="layui-form-label">排序</label>' +
			'<div class="layui-input-inline">' +
			'<input type="number" name="sort" id="sort" value="0" required lay-verify="number" placeholder="请输入序号" autocomplete="off" class="layui-input nc-len-short" min="0" max="100000">' +
			'</div>' +
			'</div>' +
			'<div class="nc-form-row">' +
			'<button class="layui-btn" lay-submit lay-filter="formDome">保存</button>' +
			'<button type="reset" class="layui-btn layui-btn-primary" onclick="back()">返回</button>' +
			'</div>' +
			'</div>';
		layer.open({
			type: 1,
			title: '添加会员等级',
			skin: 'layer-tips-class',
			area: ['500px'],
			closeBtn: 0,
			content: html,
			success: function (layero, index) {
				formDome();
			}
		})
	}
	
	/**
	 * 调整排序
	 */
	function setSort(element, level_id) {
		var value = $(element).val();
		if (value != '' && /\D/.test(value)) {
			layer.msg("排序序号请输入正整数");
			$(element).focus();
			return false;
		}
		$.ajax({
			type : "post",
			url : '{:addon_url("sitehome/member/setsort")}',
			data : {'sort': value, level_id},
			dataType: 'json',
			success : function(data){
				layer.msg(data.message);
				table.reload();
			}
		});
	}
	
	/**
	 * 表单提交
	 */
	function formDome(level_id = 0){
		layui.use('form', function(){
			layui_form = layui.form;
			var repeat_flag = false;//防重复标识
			layui_form.verify({
				number: function (value, item) {
					if (value == '') {
						return '请输入排序序号';
					}
					if (value != '' && /\D/.test(value)) {
						return "排序序号请输入正整数";
					}
				},
			});
			
			layui_form.render();
			layui_form.on('submit(formDome)', function(data){
				var field = data.field;
				if(repeat_flag) return;
				repeat_flag = true;
				$.ajax({
					type : "post",
					url : '{:addon_url("sitehome/member/addlevel")}',
					data : {field, level_id},
					dataType: 'json',
					success : function(data){
						layer.msg(data.message);
						if(data.code == 0){
							table.reload();
							layer.closeAll('page');
						}
						repeat_flag = false;
					}
				});
			});
		});
	}
</script>
{/block}